#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>
#include <iostream>
using namespace std;

static void f1( int, int, int, int );
static void f2( void );
static jmp_buf jmpbuffer;
static int globval;

int main( int argc, char *argv[] )
{
	int autoval;
	register int regival;
	volatile int volaval;
	static int statval;

	globval = 1; autoval = 2; regival =  3; volaval = 4; statval = 5;

	if( setjmp( jmpbuffer ) != 0 ) {
		cout << "after longjmp: " << endl;
		printf( "globval = %d, autoval = %d, regival = %d, volaval =%d, statval = %d\n",
				globval, autoval, regival, volaval, statval );
		exit( 0 );
	}

	globval = 91; autoval = 92; regival =  93; volaval = 94; statval = 95;

	f1( autoval, regival, volaval, statval );    // never returns
	exit( 0 );
}

static void f1( int i, int j, int k, int l )
{
	cout << "int f1():" << endl;
	printf( "globval = %d, autoval = %d, regival = %d, volaval =%d, statval = %d\n",
			globval, i, j, k, l );
	f2();
}

static void f2( void )
{
	longjmp( jmpbuffer, 1 );
}

// g++ -o longjmp.exe jongjmp.cpp
// g++ -O -o longjmp.exe jongjmp.cpp // -O 表示优化
// 通过上面两种方法编译所得到的程序运行结果不一样的。
